package com.dx.financehy.serializer;

import cn.hutool.core.util.IdcardUtil;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * 姓名脱敏序列化器
 *@JsonSerialize(using = NameHideSerializer.class)
 */
@Slf4j
public class NameHideSerializer implements ObjectSerializer {
    public static final NameHideSerializer instance = new NameHideSerializer();

    public NameHideSerializer() {
    }
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeNull();
        } else {
            out.writeString(IdcardUtil.hide(object.toString(), 1, 2));
        }
    }
}
